package webapp.backOffice.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modele.utilisateur.Entreprise;
import webapp.tools.Auth;
import dao.utilisateur.EntrepriseDAO;


@WebServlet("/admin/societes")
public class SocieteControler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
        if(!Auth.isAdmin(request.getSession()))
        {
            response.sendRedirect(request.getContextPath() + "/admin");
        }
        else
        {
        	
			String action = request.getParameter("action");
			String id = request.getParameter("userid");
			
			if(action != null)
			{
				if(action.equals("edit") && id != null)
				{
					request.setAttribute("user", _getEntreprise(Long.parseLong(id)));
		            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/societe.jsp");
		            dispatcher.forward(request, response);
				}
				else if(action.equals("insert"))
				{
		            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/societe.jsp");
		            dispatcher.forward(request, response);
				}
			}
			else
			{
				request.setAttribute("users", _getInvestisseurs());
	            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/societelist.jsp");
	            dispatcher.forward(request, response);
				
			}
        }
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*if(!Auth.isAdmin(request.getSession()))
        {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/login.jsp");
            dispatcher.forward(request, response);
        }
        else*/
		{
			String action = request.getParameter("action");
			String id = request.getParameter("userid");
			
			if(action != null)
			{
				if(action.equals("edit"))
				{
					_edit(request);
					response.sendRedirect(request.getContextPath() + "/admin/societes");
				}
				else if(action.equals("insert"))
				{
					_create(request);
					response.sendRedirect(request.getContextPath() + "/admin/societes");
				}
			}
		}
	}
	
	
	private List<Entreprise> _getInvestisseurs()
	{
		return new EntrepriseDAO().retrieveAll();
	}
	
	
	
	private Entreprise _getFromForm(HttpServletRequest request, Entreprise entr)
	{
		entr.setNom(request.getParameter("nom"));
		entr.setSecteur(request.getParameter("secteur"));
		entr.setCapitalisation(Double.parseDouble(request.getParameter("capital")));
		return entr;
	}
	
	private void _edit(HttpServletRequest request)
	{
		Entreprise ent = _getEntreprise(Long.parseLong(request.getParameter("userid")));
		_getFromForm(request, ent);
		new EntrepriseDAO().update(ent);
	}
	
	private void _create(HttpServletRequest request)
	{
		Entreprise ent = new Entreprise();
		_getFromForm(request, ent);
		new EntrepriseDAO().create(ent);
	}
	
	private Entreprise _getEntreprise(long id)
	{
		return new EntrepriseDAO().retrieve(id);
	}

}
